//
//  DiseaseForMaintainForIpadVC.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/3/17.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "DiseaseForMaintainForIpadVC.h"
#import "SearchForDiseaseListIPadCell.h"
#import "PutNormolForIPadCell.h"
#import "SearchForDiseaseAddPicsForIPadCell.h"
#import "SearchForDiseaseDetailForIPadCell.h"
#import "PutUpFileForIPadCell.h"
#define ListCellID @"cellid1"
#define PutCellID  @"cellid2"
#define PicCellID  @"cellid3"
#define DetailCellID @"cellid4"
#define PutFileCellID @"cellid5"
#define kAlphaNum @".0123456789"
@interface DiseaseForMaintainForIpadVC ()<DetailForIPadCellDelegate,PicsForIPadDelegate,UIAlertViewDelegate>
{
    NSInteger _selectedIndex;//选中查看第几个
    NSString * _status;//0 保存  1  上报
    UIAlertView *startAlert;
        UIButton * saveBtn;
        UIButton * submit;
        UIButton * safeBtn;
    
}
@property (nonatomic) NSInteger state;//0未上报  1已上报 2已安全登记
@property (nonatomic,strong) NSString *codeId;
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,strong) NSArray *contentArray;
@property (nonatomic,strong) UIView *rightFooterView;
@property (nonatomic,strong) NSDictionary *dic;//获取的需要展示的ID字典
@property (nonatomic,strong) NSMutableArray *noticeArray;

@property (nonatomic,strong) NSMutableArray *weatherArray;
@property (nonatomic,strong) NSString *weatherId;
@property (nonatomic,strong) NSString *weatherName;
@property (nonatomic,strong) NSString *startTime;
@property (nonatomic,strong) NSString *endTime;
@property (nonatomic,strong) NSMutableArray *imgArray;
@property (nonatomic,strong) NSString *detail;
@property (nonatomic,strong) NSString *directorName;
@property (nonatomic,strong) NSString *directorId;
@property (nonatomic,strong) NSString *checkerName;
@property (nonatomic,strong) NSString *checkerId;
@property (nonatomic,strong) NSString *num;
@property (nonatomic,strong) NSString *divideTypeName;
@property (nonatomic,strong) NSString *divideTypeId;
@property (nonatomic,strong) NSString *measureUnitCode;
@property (nonatomic,strong) NSString *maxPrice;
@property (nonatomic,strong) NSString *price;
@property (nonatomic,strong) NSString *subProjectTypeCode;
@property (nonatomic,strong) NSString *subProjectTypeName;
@property (nonatomic,strong) NSString *maintenanceMeasureId;
@property (nonatomic,strong) NSString *maintenanceMeasureCode;
@property (nonatomic,strong) NSString *measureName;
@property (nonatomic,strong) NSString *measureNo;

@property (nonatomic,strong) NSMutableArray * deleteArray;//删除的图片数组
@end

@implementation DiseaseForMaintainForIpadVC

#pragma mark - Life cycle
- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self resetNav];
    [self setTableViewFooterView];
    [MBProgressHUD showHUDAddedTo:self.tableView animated:YES];
    [self setWebData];
}
#pragma mark - Property
- (NSArray *)titleArray{
    if (_titleArray==nil) {
        _titleArray = @[@"维修记录编码",@"病害巡查编码",@"通知单编码",@"损坏类型",@"维修部门",@"维修开始日期",@"维修结束日期",@"天气",@"分项工程类型",@"项目工程编码",@"细目编码",@"细目名称",@"细目控制价(元)",@"计量单位",@"单价(元)",@"实际数量",@"检查人",@"负责人",@"现场维修图片",@"备注"];
    }
    return _titleArray;
}
- (NSMutableArray *)noticeArray{
    if (_noticeArray == nil) {
        _noticeArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _noticeArray;
}

- (NSMutableArray *)weatherArray{
    if (_weatherArray == nil) {
        _weatherArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _weatherArray;
}
- (NSMutableArray *)imgArray{
    if (_imgArray==nil) {
        _imgArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _imgArray;
}
- (NSMutableArray *)deleteArray{
    if (_deleteArray == nil) {
        _deleteArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _deleteArray;
}
#pragma mark - UI
- (void)resetNav{
    self.leftTitle = @"首页";
    self.pullRefreshEnable = YES;
    self.pullLoadMoreEnable = YES;
    self.myTitle.text = @"维修记录";
    [self.tableView registerClass:[SearchForDiseaseListIPadCell class] forCellReuseIdentifier:ListCellID];
    [self.rightTableView registerClass:[PutNormolForIPadCell class] forCellReuseIdentifier:PutCellID];
    [self.rightTableView registerClass:[SearchForDiseaseAddPicsForIPadCell class] forCellReuseIdentifier:PicCellID];
    [self.rightTableView registerClass:[SearchForDiseaseDetailForIPadCell class] forCellReuseIdentifier:DetailCellID];
    [self.rightTableView registerClass:[PutUpFileForIPadCell class] forCellReuseIdentifier:PutFileCellID];
}
- (void)setTableViewFooterView{
   
    
    self.rightFooterView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.rightTableView.frame.size.width, 212*PadHeightRate)];
    self.rightFooterView.backgroundColor = Bai_Color;
    self.rightTableView.tableFooterView = self.rightFooterView;
    
    saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    saveBtn.layer.cornerRadius = 5;
    saveBtn.layer.masksToBounds = YES;
    saveBtn.layer.borderColor = [MAIN_Color CGColor];
    saveBtn.layer.borderWidth = 1;
    saveBtn.backgroundColor = Bai_Color;
    [saveBtn setTitleColor:MAIN_Color forState:UIControlStateNormal];
    [saveBtn setTitle:@"保存" forState:UIControlStateNormal];
    [saveBtn addTarget:self action:@selector(save) forControlEvents:UIControlEventTouchUpInside];
    saveBtn.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:saveBtn];
    [saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-139*PadHeightRate);
        make.height.mas_equalTo(42*PadHeightRate);
        make.left.mas_equalTo(22*PadWidthRate);
        make.right.equalTo(self.rightFooterView.mas_centerX).offset(-15*PadWidthRate);
    }];
    
    
    submit = [UIButton buttonWithType:UIButtonTypeCustom];
    submit.layer.cornerRadius = 5;
    submit.layer.masksToBounds = YES;
    submit.backgroundColor = MAIN_Color;
    [submit setTitleColor:Bai_Color forState:UIControlStateNormal];
    [submit setTitle:@"上报" forState:UIControlStateNormal];
    [submit addTarget:self action:@selector(submit) forControlEvents:UIControlEventTouchUpInside];
    submit.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:submit];
    [submit mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-139*PadHeightRate);
        make.height.mas_equalTo(42*PadHeightRate);
        make.right.mas_equalTo(-22*PadWidthRate);
        make.left.equalTo(self.rightFooterView.mas_centerX).offset(15*PadWidthRate);
    }];
    
    safeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    safeBtn.layer.cornerRadius = 5;
    safeBtn.layer.masksToBounds = YES;
    safeBtn.backgroundColor = MAIN_Color;
    [safeBtn setTitleColor:Bai_Color forState:UIControlStateNormal];
    [safeBtn setTitle:@"安全登记" forState:UIControlStateNormal];
    [safeBtn addTarget:self action:@selector(safeClick) forControlEvents:UIControlEventTouchUpInside];
    safeBtn.titleLabel.font = [UIFont systemFontOfSize:17*PadZitiRate];
    [self.rightFooterView addSubview:safeBtn];
    [safeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-135*PadHeightRate);
        make.height.mas_equalTo(45*PadHeightRate);
        make.right.mas_equalTo(-30*PadWidthRate);
        make.left.mas_equalTo(30*PadWidthRate);
    }];
    
}
#pragma mark - UI Response
- (void)save{
    _status = @"0";
    [self putData];
}
- (void)submit{
    _status = @"1";
    [self putData];
}
- (void)putData{
    if([self.weatherId length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"天气不能为空。" animated:YES];
    }else if([self.subProjectTypeCode length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"分项工程类型为空。" animated:YES];
    }else if([self.maintenanceMeasureId length]==0){
        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"项目工程编码不能为空。" animated:YES];
    } else{
        if ([_status integerValue]==1) {
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"上报" message:@"确定要上报此维修单吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }else{
            [self endPutData];
        }
        
    }
    
}
- (void)endPutData{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableArray * addImgArray = [[NSMutableArray alloc]initWithCapacity:1];
    for (int i = 0; i<self.imgArray.count; i++) {
        if ([self.imgArray[i] isKindOfClass:[UIImage class]]) {
            [addImgArray addObject:self.imgArray[i]];
        }
    }
    [NetWorkTool putUpDiseaseMaintain:self.dic[@"maintentanceRecordId"] maintainUnitId:self.dic[@"maintainUnitId"]  maintentanceNoticeNo:self.dic[@"maintentanceNoticeNo"] maintenanceDepartmentId:self.dic[@"maintenanceDepartmentId"] startDate:self.startTime endDate:self.endTime checkUser:self.checkerId directorUser:self.directorId remark:self.detail weather:self.weatherId state:_status subProject:self.subProjectTypeCode measureCode:self.maintenanceMeasureId maxPrice:self.maxPrice unit:self.measureUnitCode price:self.price num:self.num  block:^(id data) {
        if (data) {
            if ([data[@"success"]integerValue]) {
                
                [NetWorkTool userHeadImg:addImgArray deleteArr:self.deleteArray billId:self.dic[@"maintentanceRecordId"] billType:@"dmMaintentanceRecord" success:^(NSDictionary *responseObject) {
                   [MBProgressHUD hideAllHUDsForView:self.view animated:NO];

                    if ([_status integerValue]==0) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"保存成功" animated:YES];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"上报成功" animated:YES];
                    }
                    
                    self.page = 1;
                    [self setWebData];
                } failure:^(NSError *error) {
                   [MBProgressHUD hideAllHUDsForView:self.view animated:NO];

                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
                } uploadProgressBlock:^(float uploadPercent, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
                    
                }];
                
            }else{
                  [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }            }
        }else{
             [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
        }
    }];
    
}
- (void)safeClick{
    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"确认" message:@"确定要安全登记吗?" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
    
}
- (void)normolState:(NSInteger)state{
    if (self.state==2) {
        self.rightTableView.tableFooterView.hidden = YES;
        
    }else if (self.state==1){
        self.rightTableView.tableFooterView.hidden = NO;
        saveBtn.hidden = YES;
        submit.hidden = YES;
        safeBtn.hidden = NO;
        
    }else{
        self.rightTableView.tableFooterView.hidden = NO;
        saveBtn.hidden = NO;
        submit.hidden = NO;
        safeBtn.hidden = YES;
    }
    self.myCodeTitle.text = self.dataArray[_selectedIndex][@"billNo"];
}
#pragma mark - Data handle
- (void)setWebData{
    [NetWorkTool getDiseaseMaintainWithPage:self.page pageSize:10 block:^(id data) {
        [self performSelector:@selector(MJRefreshStop) withObject:nil afterDelay:1];
        [MBProgressHUD hideAllHUDsForView:self.tableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                if (self.page == 1) {
                    self.tableView.contentOffset = CGPointMake(0, 0);
                    [self.dataArray removeAllObjects];
                    _selectedIndex = 0;
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }else{
                    if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                        [self.dataArray addObjectsFromArray:data[@"data"][@"items"]];
                    }
                }
                if ([data[@"data"][@"items"]isKindOfClass:[NSArray class]]) {
                    if ([data[@"data"][@"items"] count]<10&&(self.dataArray.count!=0)) {
                        [self.tableView.mj_footer endRefreshingWithNoMoreData];
                    }
                }
                if (self.dataArray.count == 0) {
                    self.noDataView.hidden = NO;
                    self.rightTableView.tableFooterView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.codeId = self.dataArray[0][@"maintentanceRecordId"];
                    NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[0][@"vcBooleanCode"]];
                    if ([str integerValue]==2) {
                        self.state = 2;
                    }else if ([str integerValue]==1) {
                        self.state = 1;
                    }else{
                    self.state = 0;
                    }
                    [self setWebDataDetail];
                }
                [self normolState:self.state];
                [self.tableView reloadData];
                self.rightTableView.contentOffset = CGPointMake(0, 0);
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
            
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES
             ];
        }
    }];
}
- (void)setWebDataDetail{
    self.rightTableView.contentOffset = CGPointMake(0, 0);
    [self.rightTableView reloadData];
    [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
    [NetWorkTool getDiseaseMaintainDetailWithID:self.codeId block:^(id data) {
    [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
        if (data) {
            if ([data[@"success"] integerValue]) {
                self.dic = data[@"data"][@"DmMaintentanceRecordList"];
                self.startTime = self.dic[@"startDate"];
                self.endTime = self.dic[@"endDate"];
                [self.weatherArray removeAllObjects];
                [self.weatherArray addObjectsFromArray: data[@"data"][@"DictWeatherCodeList"]];
                [self.noticeArray removeAllObjects];
                [self.noticeArray addObjectsFromArray:data[@"data"][@"noticeLists"]];
                self.weatherId = self.dic[@"weather"];
                self.weatherName = self.dic[@"weatherName"];
                self.detail = self.dic[@"remark"];
                
                
                self.num = [NSString stringWithFormat:@"%@",self.dic[@"actualNum"]];
                self.subProjectTypeCode = self.dic[@"subProjectTypeCode"];
                self.subProjectTypeName = self.dic[@"subProjectTypeName"];
                self.maintenanceMeasureCode = self.dic[@"maintenanceMeasureCode"];
                self.maintenanceMeasureId = self.dic[@"maintenanceMeasureId"];
                self.measureName = self.dic[@"measureName"];
                self.measureNo = self.dic[@"measureNo"];
                self.measureUnitCode = self.dic[@"measureUnitCode"];
                self.maxPrice = [NSString stringWithFormat:@"%@",self.dic[@"maxPrice"]];
                self.price = [NSString stringWithFormat:@"%@",self.dic[@"price"]];
                
                self.directorName = self.dic[@"directorUserName"];
                self.directorId = self.dic[@"directorUser"];
                self.checkerId = self.dic[@"checkerUser"];;
                self.checkerName = self.dic[@"checkerUserName"];

                
                [self.imgArray removeAllObjects];
                if ([self.dic[@"filePath"] length]==0) {
                    
                }else{
                    [self.imgArray addObjectsFromArray: [self.dic[@"filePath"] componentsSeparatedByString:@","]];
                }

                [self.rightTableView reloadData];
            }else{
                if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                    [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                }
            }
        }else{
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，请检查网络后重试。" animated:YES];
        }
    }];
}
- (void)gotoLogin{
    [PublicMethod noTokenToLoginWithNav:self.navigationController];
}

#pragma mark - Delegate
- (void)picsForIPadWithImgArray:(NSMutableArray *)arr AndDeleteArray:(NSMutableArray *)deleteArray{
    [self.imgArray removeAllObjects];
    [self.imgArray addObjectsFromArray:arr];
    [self.deleteArray removeAllObjects];
    [self.deleteArray addObjectsFromArray:deleteArray];
}
- (void)detailForIPadCellWithString:(NSString *)str indexPath:(NSIndexPath *)indexPath{
    self.detail = str;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.tableView]) {
        
        return self.dataArray.count;
        
    }else{
        
        if (self.state!=0 &&[self.imgArray count]==0) {
            return 19;
        }else{
            return 20;
        }
        
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = (SearchForDiseaseListIPadCell *)[self tableView:self.tableView cellForRowAtIndexPath:indexPath];
        return cell.cellHeight;
    }else{
        if (indexPath.row==0) {
            return 68*PadHeightRate;
        }else if(indexPath.row == 18){
            return 146*PadHeightRate;
        }else if(indexPath.row == 19){
            return 152*PadHeightRate;
        }else{
            return 50*PadHeightRate;
        }
    }
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        SearchForDiseaseListIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:ListCellID];
        if (_selectedIndex == indexPath.row) {
            cell.contentView.backgroundColor = Bai_Color;
            
            cell.contentView.layer.borderWidth = 0;
            cell.blueLine.hidden = NO;
        }else{
            cell.contentView.backgroundColor = [PublicMethod colorWithHexString:@"#f4f4f4" andAlpha:1];
            cell.contentView.layer.borderColor = [Line_Color CGColor];
            cell.contentView.layer.borderWidth = 1;
            cell.blueLine.hidden = YES;
        }
        cell.secondLab.font = [UIFont systemFontOfSize:11*PadZitiRate];
        [cell diseaseMaintainAddValue:self.dataArray[indexPath.row]];
        return cell;
    }else{
        if (indexPath.row == 18) {
            if (self.state !=0&&[self.imgArray count]==0) {
                SearchForDiseaseDetailForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
                cell.titleLab.text = @"备注";
                cell.delegate = self;
                cell.indexPath = indexPath;
                if (self.state !=0) {
                    cell.detailTextView.editable = NO;
                    if ([self.detail length]==0) {
                        cell.detailTextView.text = @" ";
                    }else{
                        cell.detailTextView.text= self.detail;
                    }
                }else{
                    cell.detailTextView.editable = YES;
                    cell.detailTextView.placeholder = @"请输入备注内容";
                    cell.detailTextView.text = self.detail;
                }
                return cell;
            }else{
                SearchForDiseaseAddPicsForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:PicCellID];
                cell.delegate = self;
                if (self.state!=0) {
                    cell.isReset = NO;
                }else{
                    cell.isReset = YES;
                }
                cell.titleLab.text = @"现场维修图片";
                cell.con = self;
                [cell addValue:self.imgArray];
                return cell;
            }
        }else if(indexPath.row == 19){
            SearchForDiseaseDetailForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellID];
            cell.titleLab.text = @"备注";
            cell.delegate = self;
            cell.indexPath = indexPath;
            if (self.state !=0) {
                cell.detailTextView.editable = NO;
                if ([self.detail length]==0) {
                    cell.detailTextView.text = @" ";
                }else{
                    cell.detailTextView.text= self.detail;
                }
            }else{
                cell.detailTextView.editable = YES;
                cell.detailTextView.placeholder = @"请输入备注内容";
                cell.detailTextView.text = self.detail;
            }
            return cell;
        }
       /* else if(indexPath.row ==8){
            PutUpFileForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:PutFileCellID];
            cell.titleLab.text = self.titleArray[indexPath.row];
            cell.contentLab.text = @"未选中文件";
            return cell;
        }
        */
        else{
            PutNormolForIPadCell *cell = [tableView dequeueReusableCellWithIdentifier:PutCellID];
            cell.titleLab.text = self.titleArray[indexPath.row];
            switch (indexPath.row) {
                case 0:
                {
                    cell.contentLab.text = self.dic[@"billNo"];
                }
                    break;
                case 1:
                {
                    cell.contentLab.text = self.dic[@"diseaseInspectionNo"];
                }
                    break;
                case 2:
                {
                    cell.contentLab.text = self.dic[@"maintentanceNoticeNo"];
                    
                }
                    break;
                case 3:
                {
                    cell.contentLab.text = self.dic[@"diseaseTypeName"];
                }
                    break;
                case 4:
                {
                    cell.contentLab.text = self.dic[@"maintenanceDepartment"];
                }
                    break;
                case 5:
                {
                    cell.contentLab.text = self.startTime;
                }
                    break;
                case 6:
                {
                    cell.contentLab.text = self.endTime;
                }
                    break;
                case 7:
                {
                    cell.contentLab.text = self.weatherName;
                }
                    break;
                case 8:
                {
                    cell.contentLab.text = self.subProjectTypeName;
                }
                    break;
                case 9:
                {
                    cell.contentLab.text = self.maintenanceMeasureCode;
                }
                    break;
                case 10:
                {
                    cell.contentLab.text = self.measureNo;
                }
                    break;
                case 11:
                {
                    cell.contentLab.text = self.measureName
                    ;
                }
                    break;
                case 12:
                {
                    
                    cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.maxPrice doubleValue]]];
                    [cell.titleLab mas_updateConstraints:^(MASConstraintMaker *make) {
                        make.centerY.mas_equalTo(0);
                        make.height.mas_equalTo(36*PadHeightRate);
                        make.left.mas_equalTo(12*PadWidthRate);
                        make.width.mas_equalTo(120*PadWidthRate);
                    }];
                }
                    break;
                case 13:
                {
                    cell.contentLab.text = self.measureUnitCode;
                }
                    break;
                case 14:
                {
                    cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.price doubleValue]]];
                }
                    break;
                case 15:
                {
                    cell.contentLab.text = [PublicMethod changeFloat:[NSString stringWithFormat:@"%.6f",[self.num doubleValue]]];
                }
                    break;
                case 16:
                {
                    cell.contentLab.text = self.checkerName;
                }
                    break;
                case 17:
                {
                    cell.contentLab.text = self.directorName;
                }
                    break;
                default:
                    break;
            }

            if (self.state != 0) {
                cell.img.hidden = YES;
            }else{
            if (indexPath.row==5||indexPath.row==6||indexPath.row==7||indexPath.row==17||indexPath.row==16||indexPath.row==15||indexPath.row==8||indexPath.row==9) {
                cell.img.hidden = NO;
                cell.img.image = [UIImage imageNamed:@"enter"];
            }else{
                cell.img.hidden = YES;
            }
            }
            return cell;
        }
    }
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.tableView]) {
        _selectedIndex = indexPath.row;
        [self.tableView reloadData];
        self.codeId = self.dataArray[indexPath.row][@"maintentanceRecordId"];
        NSString *str = [NSString stringWithFormat:@"%@",self.dataArray[indexPath.row][@"vcBooleanCode"]];
        if ([str integerValue]==2) {
            self.state = 2;
        }else if ([str integerValue]==1) {
            self.state = 1;
        }else{
            self.state = 0;
        }
        [self setWebDataDetail];
        [self normolState:self.state];
    }else{
        if (self.state==0) {
            if (indexPath.row==7) {
                UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"天气" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:indexPath];
                for (int i = 0; i<self.weatherArray.count; i++) {
                    [actionSheet addAction:[UIAlertAction actionWithTitle:self.weatherArray[i][@"itemName"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.weatherName = self.weatherArray[i][@"itemName"];
                        self.weatherId = self.weatherArray[i][@"itemCode"];
                        cell.contentLab.text = self.weatherName;
                    }]];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionSheet.popoverPresentationController.sourceView = cell.bgView;
                    actionSheet.popoverPresentationController.sourceRect = CGRectMake(cell.bgView.frame.size.width-15*PadWidthRate,(CGFloat)cell.bgView.frame.size.height/(CGFloat)2.0,1.0,1.0);
                    [self presentViewController:actionSheet animated:YES completion:nil];
                });
               
            }
            if(indexPath.row ==5){
                //x,y 值无效，默认是居中的
                KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
                
                //配置中心，详情见KSDatePikcerApperance
                
                picker.appearance.radius = 5;
                
                //设置回调
                picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
                    
                    if (buttonType == KSDatePickerButtonCommit) {
                        
                        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                        [formatter setDateFormat:@"yyyy-MM-dd"];
                        self.startTime = [formatter stringFromDate:currentDate];
                        PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:indexPath];
                        cell.contentLab.text = self.startTime;
                    }
                };
                // 显示
                [picker show];
            }
            if (indexPath.row == 6) {
                //x,y 值无效，默认是居中的
                KSDatePicker* picker = [[KSDatePicker alloc] initWithFrame:CGRectMake(0, 0, 280, 260)];
                
                //配置中心，详情见KSDatePikcerApperance
                
                picker.appearance.radius = 5;
                
                //设置回调
                picker.appearance.resultCallBack = ^void(KSDatePicker* datePicker,NSDate* currentDate,KSDatePickerButtonType buttonType){
                    
                    if (buttonType == KSDatePickerButtonCommit) {
                        
                        NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
                        [formatter setDateFormat:@"yyyy-MM-dd"];
                        self.endTime = [formatter stringFromDate:currentDate];
                        PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:indexPath];
                        cell.contentLab.text = self.endTime;
                    }
                };
                // 显示
                [picker show];
            }
            if (indexPath.row==8) {
                UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"分项工程类型" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                 PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:indexPath];
                for (int i = 0; i<self.noticeArray.count; i++) {
                    [actionSheet addAction:[UIAlertAction actionWithTitle:self.noticeArray[i][@"subProjectTypeName"] style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                        self.subProjectTypeName = self.noticeArray[i][@"subProjectTypeName"];
                        self.subProjectTypeCode = self.noticeArray[i][@"subProjectTypeCode"];
                        self.maintenanceMeasureCode = nil;
                        self.maintenanceMeasureId = nil;
                        self.measureNo = nil;
                        self.measureName = nil;
                        self.maxPrice = nil;
                        self.measureUnitCode = nil;
                        self.price = nil;
                        [self.rightTableView reloadData];
                        
                    }]];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    actionSheet.popoverPresentationController.sourceView = cell.bgView;
                    actionSheet.popoverPresentationController.sourceRect = CGRectMake(cell.bgView.frame.size.width-15*PadWidthRate,(CGFloat)cell.bgView.frame.size.height/(CGFloat)2.0,1.0,1.0);
                    [self presentViewController:actionSheet animated:YES completion:nil];
                });
                
            }
            if (indexPath.row==9) {
                if (self.subProjectTypeCode) {
                    [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
                    [NetWorkTool getProjectCodeWithSubProjectTypeCode:self.subProjectTypeCode block:^(id data) {
                        [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
                        if (data) {
                            if([data[@"success"] integerValue]){
                                UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"项目工程编码" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                                PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:indexPath];
                                for (int i = 0; i<[data[@"data"] count]; i++) {
                                    [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"maintenanceMeasureCode"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                        self.maintenanceMeasureCode = data[@"data"][i][@"maintenanceMeasureCode"];
                                        self.maintenanceMeasureId = data[@"data"][i][@"maintenanceMeasureId"];
                                        self.measureName = data[@"data"][i][@"measureName"];
                                        self.measureNo = data[@"data"][i][@"measureNo"];
                                        self.maxPrice = data[@"data"][i][@"maxPrice"];
                                        self.measureUnitCode = data[@"data"][i][@"measureUnitCode"];
                                        self.price = data[@"data"][i][@"price"];
                                        [self.rightTableView reloadData];
                                    }]];
                                }
                               
                                dispatch_async(dispatch_get_main_queue(), ^{
                                    actionSheet.popoverPresentationController.sourceView = cell.bgView;
                                    actionSheet.popoverPresentationController.sourceRect = CGRectMake(cell.bgView.frame.size.width-15*PadWidthRate,(CGFloat)cell.bgView.frame.size.height/(CGFloat)2.0,1.0,1.0);
                                    [self presentViewController:actionSheet animated:YES completion:nil];
                                });
                                
                            }else{
                                
                                [PublicMethod noTokenToLoginWithNav:self.navigationController];
                            }
                            
                        }else{
                            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取项目工程编码失败" animated:YES];
                        }
                        
                    }];
                    
                    
                }else{
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"请先选择分项工程类型" animated:YES];
                    
                }
            }
            if (indexPath.row == 16) {
                [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
                [NetWorkTool receiveManWithUnitID:nil block:^(id data) {
                    [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
                    if (data) {
                        if([data[@"success"] integerValue]){
                            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"检查人" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                            PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:16 inSection:0]];
                            for (int i = 0; i<[data[@"data"] count]; i++) {
                                [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"userName"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                    
                                    self.checkerName = data[@"data"][i][@"userName"];
                                    self.checkerId = data[@"data"][i][@"userId"];
                                    cell.contentLab.text = self.checkerName;
                                    
                                }]];
                            }
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                actionSheet.popoverPresentationController.sourceView = cell.bgView;
                                actionSheet.popoverPresentationController.sourceRect = CGRectMake(cell.bgView.frame.size.width-15*PadWidthRate,(CGFloat)cell.bgView.frame.size.height/(CGFloat)2.0,1.0,1.0);
                                [self presentViewController:actionSheet animated:YES completion:nil];
                            });
                            
                        }else{
                            
                            [PublicMethod noTokenToLoginWithNav:self.navigationController];
                        }
                        
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取检查人失败" animated:YES];
                    }
                    
                }];
                
            }

            if (indexPath.row == 17) {
                [MBProgressHUD showHUDAddedTo:self.rightTableView animated:YES];
                [NetWorkTool receiveManWithUnitID:nil block:^(id data) {
                    [MBProgressHUD hideAllHUDsForView:self.rightTableView animated:NO];
                    if (data) {
                        if([data[@"success"] integerValue]){
                            UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"负责人" message:nil preferredStyle:UIAlertControllerStyleActionSheet];
                            PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:17 inSection:0]];
                            for (int i = 0; i<[data[@"data"] count]; i++) {
                                [actionSheet addAction:[UIAlertAction actionWithTitle:[NSString stringWithFormat:@"%@",data[@"data"][i][@"userName"]]style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                                    
                                    self.directorName = data[@"data"][i][@"userName"];
                                    self.directorId = data[@"data"][i][@"userId"];
                                    cell.contentLab.text = self.directorName;
                                    
                                }]];
                            }
                            
                            dispatch_async(dispatch_get_main_queue(), ^{
                                actionSheet.popoverPresentationController.sourceView = cell.bgView;
                                actionSheet.popoverPresentationController.sourceRect = CGRectMake(cell.bgView.frame.size.width-15*PadWidthRate,(CGFloat)cell.bgView.frame.size.height/(CGFloat)2.0,1.0,1.0);
                                [self presentViewController:actionSheet animated:YES completion:nil];
                            });
                            
                        }else{
                            
                            [PublicMethod noTokenToLoginWithNav:self.navigationController];
                        }
                        
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"因网络问题获取负责人失败" animated:YES];
                    }
                    
                }];
                
            }
        
            if (indexPath.row == 15) {
                startAlert = [[UIAlertView alloc]initWithTitle:@"请输入实际数量" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
                [startAlert setAlertViewStyle:UIAlertViewStylePlainTextInput];
                UITextField *startField = [startAlert textFieldAtIndex:0];
                startField.keyboardType = UIKeyboardTypeDecimalPad;
                startField.placeholder = @"请输入实际数量";
                if ([self.num doubleValue]==0) {
                    startField.text = nil;
                }else{
                    startField.text = self.num;
                }
                [startField becomeFirstResponder];
                [startAlert show];
            }
        }

    }
    
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if ([alertView.title isEqualToString:@"上报"]) {
        if (buttonIndex) {
            [self endPutData];
        }
    }else if([alertView.title isEqualToString:@"确认"]){
        [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        
        [NetWorkTool securityRegisterWithMaintentanceRecordId:self.dic[@"maintentanceRecordId"] block:^(id data) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:NO];
            if (data) {
                if ([data[@"success"]integerValue]) {
                    
                    
                    [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"安全登记成功" animated:YES];
                    
                    
                    self.page = 1;
                    [self setWebData];
                    
                    
                }else{
                    if ([data[@"message"] isEqualToString:@"请求需要进行验证,请重新传入Token"]) {
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"账号被其他设备登录，请重新登录" animated:YES];
                        [self performSelector:@selector(gotoLogin) withObject:nil afterDelay:1];
                    }else{
                        [MBProgressHUD showTextHUDAddedTo:self.view Msg:data[@"message"] animated:YES];
                    }
                }
            }else{
                
                [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"网络错误，操作失败。" animated:YES];
            }
        }];
        
    }else  {
    if (buttonIndex == alertView.firstOtherButtonIndex) {
        UITextField* textField = [alertView textFieldAtIndex:0];
        if([textField.text removeSpace].length==0){
            [MBProgressHUD showTextHUDAddedTo:self.view Msg:@"输入数量为空" animated:YES];
            return;
        }else{
            NSCharacterSet *cs;
            cs = [[NSCharacterSet characterSetWithCharactersInString:kAlphaNum] invertedSet];
            
            NSString *filtered = [[textField.text componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""]; //按cs分离出数组,数组按@""分离出字符串
            self.num =[PublicMethod changeFloat:[NSString stringWithFormat:@"%f",[filtered doubleValue]]];
            
            PutNormolForIPadCell *cell = (PutNormolForIPadCell *)[self.rightTableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:15 inSection:0]];
            cell.contentLab.text = self.num;
        }
    }
    }
}
@end
